// lives/anchor/anchor.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    anchor_id: null,
    isStop: false,
    page: 1,
    user: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.anchor_id) {
      this.setData({
        anchor_id: options.anchor_id
      })
      this.getList()
    }
  },

  /**
     * 点击直播
     */
    onClickLive: function (e) {
      let item = e.currentTarget.dataset.item
      wx.navigateTo({
          url: '/lives/live/live?room_id=' + item.id
      })
  },

  /**
     * 获取列表
     */
    getList: function () {
      wx.showLoading({
          title: '加载中...',
      });
      this.setData({
          isStop: false
      })
      getApp().request({
          url: getApp().api.live.get_anchor_room_list,
          data: {
              anchor_id: this.data.anchor_id
          },
          success: data => {
              if (data.code == 0) {
                  this.setData({
                      list: data.data.list,
                      user: data.data.user
                  })
              } else {
                  wx.showModal({
                      title: "提示",
                      content: data.msg,
                      showCancel: !1,
                      confirmText: "确认",
                      success: function (t) {
                          t.confirm && wx.navigateBack({
                              delta: 1
                          });
                      }
                  })
              }
          },
          complete: () => {
              wx.hideLoading();
          }
      });
  },

  /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      if (this.data.isStop) {
          return;
      }
      wx.showLoading({
          title: '加载中...',
      });
      getApp().request({
          url: getApp().api.live.get_anchor_room_list,
          data: {
              page: this.data.page + 1,
              anchor_id: this.data.anchor_id
          },
          success: data => {
              if (data.code == 0) {
                  if (data.data.list.length > 0) {
                      this.setData({
                          list: this.data.list.concat(data.data.list),
                          page: this.data.page + 1
                      });
                  } else {
                      this.setData({
                          isStop: true
                      });
                  }

              } else {
                  wx.showToast({
                      title: data.msg,
                      icon: 'none'
                  });
              }
          },
          complete: () => {
              wx.hideLoading();
          }
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})